package com.qdxwx.web.controller.picture;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKRequest;
import com.qdxwx.data.service.category.CategoryService;
import com.qdxwx.data.service.channel.ChannelService;
import com.qdxwx.data.service.favorite.FavoriteService;
import com.qdxwx.data.service.member.MemberRecordService;
import com.qdxwx.data.service.picture.PictureAlbumService;
import com.qdxwx.data.service.picture.PictureService;
import com.qdxwx.data.service.subject.SubjectService;
import com.qdxwx.models.category.Category;
import com.qdxwx.models.channel.Channel;
import com.qdxwx.models.enums.ModuleType;
import com.qdxwx.models.member.MemberRecord;
import com.qdxwx.models.picture.Picture;
import com.qdxwx.models.picture.PictureAlbum;
import com.qdxwx.models.picture.PictureAlbumVo;
import com.qdxwx.models.picture.PictureQuery;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/picture")
public class PictureViewController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(PictureViewController.class);

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private PictureService pictureService;
    @Autowired
    private ChannelService channelService;
    @Autowired
    private PictureAlbumService pictureAlbumService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private FavoriteService favoriteService;
    @Autowired
    private MemberRecordService memberRecordService;

    @RequestMapping("/view/{id:[0-9]+}.html")
    public String pictureView(@PathVariable("id") int pictureId, HttpServletRequest request, ModelMap mmap) {

        Picture picture = pictureService.get(pictureId);
        if (picture == null) {
            return toError404(request);
        }
        Channel channel = channelService.get(picture.getChannelId());
        if (channel == null) {
            return toErrorView(request, "找不到对应的频道！");
        }
        //禁用的频道
        if (channel.getIsDisabled() == 1) {
            return toErrorView(request, "当前频道已被禁用！");
        }
        //判断是否属于模块
        if (channel.getModuleId() != ModuleType.PICTURE.ordinal()) {
            logger.error("当前频道归属错误！");
            logger.error(channel.toString());
            return toErrorView(request, "当前频道归属错误！");
        }

        PictureQuery query = new PictureQuery();
        query.setStatus(1);
        query.setCategoryId(picture.getCategoryId());
        query.setOrderBy(3);
        List<Picture> pictureList = pictureService.getList(query, new PageRequest(1, 12));
        Category category = categoryService.get(picture.getCategoryId());
        List<Category> categoryRoots = categoryService.getChildren(category.getParentId() > 0 ? category.getParentId() : category.getCategoryId());
        List<Integer> albumIds = pictureAlbumService.getAlbumIdsByPictureId(picture.getId());
        if (albumIds != null && albumIds.size() > 0) {
            PictureAlbumVo albumVo = new PictureAlbumVo();
            PictureAlbum album = pictureAlbumService.get(albumIds.get(0));
            BeanUtils.copyProperties(album, albumVo);
            albumVo.setPictures(pictureService.getListByAlbumId(album.getId(), new PageRequest(1, 6)));
            mmap.put("album", albumVo);
        }
        pictureService.updateView(picture.getId());

        long userId = LoginUser.getUserId(request);

        MemberRecord member = null;
        if (userId > 0) {
            //查询是否已经收藏
            boolean isFavorite = favoriteService.isExist(userId, picture.getId(), ModuleType.PICTURE.ordinal());
            member = memberRecordService.getValidByUserId(userId, 1);
            mmap.put("isFavorite", isFavorite);
            mmap.put("isBuy", false);
        } else {
            mmap.put("isFavorite", false);
            mmap.put("isBuy", false);
        }
        mmap.put("isVip", member != null);
        mmap.put("channel", channel);
        mmap.put("subjectName", subjectService.getName(picture.getSubjectId()));
        mmap.put("userId", userId);
        mmap.put("picture", picture);
        mmap.put("pictureList", pictureList);
        mmap.put("categoryRoots", categoryRoots);
        if (TKRequest.isMobileBrowse(request)){
            return getViewPathH5("picture/picture-view");
        }
        return getViewPath("picture/picture-view");
    }

    @RequestMapping("/album/view/{id:[0-9]+}.html")
    public String albumView(@PathVariable("id") int id, HttpServletRequest request, ModelMap mmap) {
        if (id < 1) return toError404(request);
        PictureAlbum album = pictureAlbumService.get(id);
        if (album == null) return toError404(request);
        List<Picture> pictureList = pictureService.getListByAlbumId(album.getId(), null);
        int pictureCount = pictureService.countByAlbumId(album.getId());
        Category category = categoryService.get(album.getCategoryId());
        List<Category> categoryRoots = categoryService.getChildren(category.getParentId() > 0 ? category.getParentId() : category.getCategoryId());
        pictureAlbumService.updateView(album.getId());
        mmap.put("album", album);
        mmap.put("albumLogo", pictureCount > 0 ? pictureList.get(0).getCoverUrl() : "");
        mmap.put("pictureCount", pictureCount);
        mmap.put("pictureList", pictureList);
        mmap.put("categoryRoots", categoryRoots);
        return getViewPath("picture/picture-album-view");
    }
}
